package com.example.order.servive.impl;

import com.example.order.domain.Order;
import com.example.order.mapper.OrderMapper;
import com.example.order.servive.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class OrderServiceImpl implements OrderService {

    @Autowired
    private OrderMapper orderMapper;

    @Override
    public Boolean create(String userId, String commodityCode, int orderCount) {
        Order order = new Order();
        order.setUserId(userId);
        order.setCommodityCode(commodityCode);
        order.setCount(orderCount);
        order.setMoney(calculateMoney(commodityCode, orderCount));
        int influenceCount = orderMapper.insertSelective(order);
        return influenceCount == 1;
    }

    @Override
    public int calculateMoney(String commodityCode, int orderCount) {
        return Integer.parseInt(commodityCode) % 10 * orderCount;
    }
}
